package com.myhome.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.myhome.model.User12;

public class transientDemo {

	public static void Test01() {
		User12 user = new User12();

		String dateString = "2018-05-04";
		DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		Date bitrh = null;
		try {
			bitrh = date.parse(dateString);
			user.setBirthday(bitrh);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		user.setLoginname("登录名");
		user.setPassword("12345678");
		System.out.println("用户名：" + user.getLoginname() + " 生日：" + user.getBirthday() + " 密码：" + user.getPassword());
		try {
			ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("D:/user.txt"));
			os.writeObject(user);
			os.flush();
			os.close();
		}catch(FileNotFoundException fe) {
			fe.printStackTrace();
		}
		catch(IOException ex) {
			ex.printStackTrace();
		}
		try {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream("D:/user.txt"));
            user = (User12) is.readObject(); // 从流中读取User的数据
            is.close();
		}catch(FileNotFoundException fe) {
			fe.printStackTrace();
		}
		catch(IOException ex) {
			ex.printStackTrace();
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		System.out.println("用户名：" + user.getLoginname() + " 生日：" + user.getBirthday() + " 密码：" + user.getPassword());

	}

	public static void main(String[] args) {
		Test01();
	}
}
